package com.scut.online_black_market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scut.online_black_market.entity.ConsultationListResponse;
import com.scut.online_black_market.dto.ConsultationRequest;
import com.scut.online_black_market.dto.ConsultationResponse;
import com.scut.online_black_market.dto.SuccessResponse;
import com.scut.online_black_market.exception.UniversalException;
import com.scut.online_black_market.service.ConsultationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Optional;


@Api(value = "付费咨询接口",tags = "付费询问接口")
@RestController
@RequestMapping(value = "/api/consultation")
public class ConsultationController {

    @Resource
    private ConsultationService consultationService;

    /**
     * create by yellowBroke
     * @param consultationRequest
     * @return
     */
    @ApiOperation("发布付费咨询")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseEntity<SuccessResponse> uploadConsultation(@RequestBody ConsultationRequest consultationRequest) {

        try {
            consultationService.uploadConsultation(consultationRequest);
            return ResponseEntity.ok(new SuccessResponse(true, "发布成功"));
        } catch (UniversalException e) {
            return ResponseEntity.ok(new SuccessResponse(false, e.getMessage()));
        }
    }
    /**
     * create by XC at 2020.11.19
     */
    @ApiOperation("分页查询问题")
    @RequestMapping(value="/queryConsultationByPage/{pageNo}/{pageSize}/{typeId}",method = RequestMethod.GET)
    public ResponseEntity<IPage<ConsultationListResponse>> queryShoppingGuideByPage (
            @ApiParam("页号数")@PathVariable short pageNo,
            @ApiParam("页大小")@PathVariable short pageSize,
            @ApiParam("查询种类")@PathVariable short typeId) throws UniversalException {

        IPage<ConsultationListResponse> result= consultationService.queryConsultationByPage(pageNo,pageSize,typeId);
        return ResponseEntity.ok(result);
    }

    /**
     * @wmt
     * @param consultationId
     * @return
     */
    @ApiOperation("根据问题id获取问题详情")
    @RequestMapping(value = "/queryByConsultationId/{consultationId}",method = RequestMethod.GET)
    public ResponseEntity<ConsultationResponse> queryByConsultationId(@PathVariable BigInteger consultationId){
        try{
            ConsultationResponse consultationResponse= consultationService.queryByConsultationId(consultationId);
            return ResponseEntity.ok(consultationResponse);
        }catch(UniversalException e){
            return ResponseEntity.of(Optional.empty());
        }
    }
    @ApiOperation("删除问题")
    @RequestMapping(value = "/deleteTheQuestion/{consultationId}",method = RequestMethod.POST)
    public ResponseEntity<SuccessResponse> deleteByConsultationId(@PathVariable BigInteger consultationId){
        try {
            consultationService.deleteByConsultationId(consultationId);
            return ResponseEntity.ok(new SuccessResponse(true,"删除成功"));
        }
        catch (UniversalException e){
            return ResponseEntity.ok(new SuccessResponse(false,e.getMessage()));
        }
    }
}
